package ${bean.packageName};

import com.easy.system.controller.BaseController;
import com.easy.system.util.*;
import com.easy.yours.biz.model.${bean.className};
import com.easy.yours.biz.service.${bean.className}Service;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* Created with generator tools.
* User: ${bean.createUser}
* DateTime:${bean.createTime}
*/
@Controller
public class ${bean.className}Controller extends BaseController<${bean.className}> {
    @Resource
    private ${bean.className}Service ${bean.lowerClassName}Service;

    public ${bean.className}Controller() {
    super("${bean.lowerClassName}", ${bean.className}.class);
    }

    @ResponseBody
    @RequestMapping
    protected void getGridData(HttpServletRequest request, HttpServletResponse response) throws Exception {
    super.setBizService(${bean.lowerClassName}Service);
    List<${bean.className}> entitys = null;
        Long total = null;
        Pagination pagination = RequestVarsUtil.getPagination(request);
        String type = request.getParameter("searchType");

        if (type != null) {
        if (type.equals(Constant.ADVANCE_SEARCH)) {
        //                String[] allParams = new String[]{"property1", "property2" };
        String[] allParams = null;
        entitys = super.getAdvanceQueryEntitys(request, pagination, allParams);
        total = super.getAdvanceQueryTotal(request, allParams);
        } else {
        String property = request.getParameter("searchProperty");
        String searchValue = request.getParameter("searchValue");
        //                Object[] allProperties = new Object[]{"property1", "property2" };
        Object[] allProperties = null;
        entitys = super.getSimpleQueryEntitys(property, searchValue, allProperties, pagination);
        total = super.getSimpleQueryTotal(property, searchValue, allProperties);
        }
        } else {
        entitys = super.getEntitys(pagination);
        total = super.getTotal();
        }

        ResultDto<List<${bean.className}>> resultDto = new ResultDto<List<${bean.className}>>();
            resultDto.setRows(entitys);
            resultDto.setTotal(total);
            resultDto.setSuccess(true);
            writeToClient(response, resultDto.toString());
            }

            //跳到主页
            @RequestMapping
            public String index(Model model, HttpServletRequest request) {
            super.setBizService(${bean.lowerClassName}Service);
            super.setModel(model);
            return super.toView(request);
            }

            //编辑界面
            @RequestMapping
            public String edit(Model model, HttpServletRequest request, Long id) {
            super.setBizService(${bean.lowerClassName}Service);
            ${bean.className} ${bean.lowerClassName} = super.getEntity(id);
            super.setModel(model, ${bean.lowerClassName});
            return super.toView(request);
            }

            //添加界面
            @RequestMapping
            public String add(Model model, HttpServletRequest request) {
            super.setBizService(${bean.lowerClassName}Service);
            super.setModel(model);
            return super.toView(request);
            }

            //查看界面
            @RequestMapping
            public String view(Model model, HttpServletRequest request, Long id) {
            super.setBizService(${bean.lowerClassName}Service);
            ${bean.className} ${bean.lowerClassName} = super.getEntity(id);
            super.setModel(model, ${bean.lowerClassName});
            return super.toView(request);
            }

            //保存操作
            @RequestMapping
            public void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
            super.setBizService(${bean.lowerClassName}Service);
            MessageDto messageDto = new MessageDto();
            ${bean.className} ${bean.lowerClassName} = super.injectObject(request, ${bean.className}.class);
            try {
            if (${bean.lowerClassName}.getId() != null) {
            super.updateEntity(${bean.lowerClassName});
            super.setMessageDto(messageDto, MessageDefined.update_success_info);
            } else {
            this.${bean.lowerClassName}Service.save(${bean.lowerClassName});
            super.setMessageDto(messageDto, MessageDefined.save_success_info);
            }
            } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            }
            super.writeToClient(response, messageDto.toString());
            }

            @ResponseBody
            @RequestMapping
            protected void delete(Long id, HttpServletResponse response) throws IOException {
            super.setBizService(${bean.lowerClassName}Service);
            MessageDto messageDto = new MessageDto();
            try {
            super.deleteEntity(id);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
            } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            }
            super.writeToClient(response, messageDto.toString());
            }

            @ResponseBody
            @RequestMapping
            protected void deleteMore(HttpServletRequest request, HttpServletResponse response) throws IOException {
            super.setBizService(${bean.lowerClassName}Service);
            String ids = request.getParameter("ids");
            MessageDto messageDto = new MessageDto();
            try {
            Integer count = super.deleteEntitys(ids);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
            } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            }
            super.writeToClient(response, messageDto.toString());
            }


            }
